<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link rel="stylesheet" href="css/base.css" />
    <link rel="stylesheet" href="css/login.css" />
</head>
<body>
<div>
    <div>
    <h1 class="titles">新用户注册</h1>
</div>
    <form action="">
        <div class="verify">
            <div class="resetUpwd">
                <img src="img/shouji.png" alt="">
                <input type="text" placeholder="请输入手机号码" class="resetUpwd-input" id="register-Phone"/>
            </div>
            <div class="resetUpwd">
                <img src="img/yanzheng.png" alt="">
                <input type="text" placeholder="请输入验证码" class="resetUpwd-input" id="register-code"/>
                    <button class="acquire-code" type="button" id="register-obtainCode">
                      	验证码
                    </button>
            </div>
            <div class="resetUpwd">
                <img src="img/mima.png" alt="">
                <input type="text" placeholder="请设置登录密码" class="resetUpwd-input" id="register-password"/>
            </div>
            <div class="resetUpwd">
                <img src="img/mima.png" alt="">
                <input type="text" placeholder="请再次输入密码" class="resetUpwd-input" id="register-xPassword"/>
            </div>
            <a type="button" class="c-4e" >
                <div class="logon" id="register-button">注册</div>
            </a>
            <div class="forget">
                <a href="login.html" class="c-6e">已有账号，去登录</a>
            </div>
        </div>
    </form>
</div>
<script src="js/jquery.js"></script>
<script src="js/x_rem.js"></script>
<script src="js/secondLevel.js"></script>
<script src="js/all.js"></script>
<script>
	(()=>{
		var phone = $("#register-Phone");   //电话号码
		var Password = $("#register-password");   //登录密码
		var xPassword = $("#register-xPassword")  //确认密码
		var code = $("#register-code");          //验证码
	 
 		//获取验证码
		$("#register-obtainCode").click(function(){
			var that = $(this);  //保存this
			
			if(!vali(phone, /^1[0-9]{10}$/, "请输入正确的手机号")){

			}else{
				$.ajax({
					type:"post",
					url: Url+ "/user/doCode",
					xhrFields: {
						withCredentials: true
					},
					data:{
						"mobilePhone":  phone.val()
					},
					success:function(data){
						console.log(data);
						if(data.success){
							count(that);    //让获取验证码变成倒计时
							popup("验证码发送成功");   //弹窗提示
						}else{
							popup("获取失败,请检查网络");
						} 
					},
					error:function(err){
						console.log(err);
					}
				})
			}
					
		})
			
		//注册
		$("#register-button").click(()=>{
				if(!vali(phone, /^1[0-9]{10}$/, "请输入正确的手机号")){
					
				}else if(code.val() == ""){   //如果验证码为空的话
					popup("验证码不能为空");  //就提示
				}else if(!vali(Password, /^[a-zA-Z0-9_]{6}$/ , "请输入正确的密码格式")){
					
				}else if(Password.val() != xPassword.val()){    //如果确认密码不等于登录密码
					popup("两次密码不一致");  //就提示
				}else{
					$.ajax({
						type:"post",
						url: Url + "/user/register",
						xhrFields:{
							withCredentials:true
						},
						data:{
							"userName": phone.val(),
							"password": Password.val(),
							"code":code.val()
						},
						success:function(data){
							if(data.success){  //如果返回成功
								popup(data.success);
								//跳转到登录页面
								location.href = "login.html";
							}else{   //否则
								popup(data.success);
							}
							console.log(data);
						},
						error:function(err){
							console.log(err)
						}
					});
				}
		})
		
			
			
		 
	})();
	
 
	
</script>
</body>
</html>